package com.zhao.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhao.base.TokenThread;

/**
 * 通过servlet初始化定时刷新accessToken
 * @author zhaowj
 * @date2018年1月21日 下午1:30:00
 */
@WebServlet("/tokenServlet")
public class TokenServlet extends HttpServlet {
	private static Logger log = LoggerFactory.getLogger(TokenServlet.class);  
	private static final long serialVersionUID = 1L;
    
    public void init() throws ServletException {
    	 System.out.println("初始化TokenServlet");
    	 //TokenThread.appid = getInitParameter("appid");  
         //TokenThread.appsecret = getInitParameter("appsecret");  
   
         System.out.println("weixin api appid:{}"+ TokenThread.appid);  
         System.out.println("weixin api appsecret:{}"+TokenThread.appsecret);  
         
         Properties props = new Properties();    
         try {
			props.load(TokenServlet.class.getClassLoader().getResourceAsStream("wx.properties"));
			TokenThread.appid =props.getProperty("appId"); 
	        TokenThread.appsecret =props.getProperty("appsecret"); 
	        TokenThread.mch_id=props.getProperty("mchId");
	        TokenThread.api_key=props.getProperty("apiKey");
		} catch (IOException e) {
			e.printStackTrace();
		} 
		 System.out.println("appid>>"+ props.getProperty("appId"));  
		 System.out.println("appsecret>>"+ props.getProperty("appsecret"));  
         // 未配置appid、appsecret时给出提示  
         if ("".equals(TokenThread.appid) || "".equals(TokenThread.appsecret)) {  
             log.error("获取appid失败");  
         } else {  
             new Thread(new TokenThread()).start();  
         }  
    }
}
